/*
 ============================================================================
 文件名 	: EasyBounceMainContainer.cpp
 描述		: EasyBounceMainContainer类的实现.
 ============================================================================
 */

//引用
#include <eikbtgpc.h>

//支持皮肤需要这两个头文件
#include <AknsDrawUtils.h>
#include <AknsBasicBackgroundControlContext.h>

#include <EasyBounce.rsg>

#include "EasyBounceMainContainer.h"
#include "EasyBounceAppUi.h"
#include "EasyBounceMainview.h"
#include "EasyBounce.hrh"

#include "coedef.h"

// ================= 成员函数 =======================

// ---------------------------------------------------------
// CEasyBounceMainContainer::ConstructL()
// 两阶段构造函数
// ---------------------------------------------------------
//
void CEasyBounceMainContainer::ConstructL(const TRect& aRect)
    {
    CreateWindowL();
    
    CreateLableL();
    CreateButtonsL();

   	iBgContext = CAknsBasicBackgroundControlContext::NewL( 
   			KAknsIIDQsnBgAreaMain, aRect, ETrue);
   	
    SetRect(aRect);
    ActivateL();
    }

// ---------------------------------------------------------------------------
// CEasyBounceMainContainer::~CEasyBounceMainContainer()
// 析构函数.
// ---------------------------------------------------------------------------
//
CEasyBounceMainContainer::~CEasyBounceMainContainer()
    {
    delete iLabel;
    delete iStartButton;
    delete iRankButton;
    delete iBgContext;
    }

// ---------------------------------------------------------
// CEasyBounceMainContainer::SizeChanged()
// 此函数在控件大小发生变化时被框架调用.
// ---------------------------------------------------------
//
void CEasyBounceMainContainer::SizeChanged()
    {

    iLabel->SetExtent( TPoint(10,10), TSize(0,0) );
    
    //重置背景区域
    if(iBgContext)
		{
		iBgContext->SetRect(Rect());
        if ( &Window() )
			{
			iBgContext->SetParentPos( PositionRelativeToScreen() );
			}
		}
    
    //重置按钮的大小和位置
    TRect containerRect = Rect();
    TPoint containerTL = containerRect.iTl;
    TInt h = containerRect.Height();
    TInt w = containerRect.Width();
    
    TSize buttonSize( w*1/3, h*2/15);
    
    TPoint startButtonTL ( containerTL.iX + w/3 , containerTL.iY + h/3 );
    TRect startButtonRect (startButtonTL, buttonSize);
    iStartButton->SetRect(startButtonRect);
    
    TPoint rankButtonTL ( startButtonTL.iX, startButtonTL.iY + h*3/15 );
    TRect rankButtonRect (rankButtonTL, buttonSize);
    iRankButton->SetRect(rankButtonRect);
    }

// ---------------------------------------------------------
// CEasyBounceMainContainer::CountComponentControls()
// 此函数被框架调用，返回容器中的控件个数.
// ---------------------------------------------------------
//
TInt CEasyBounceMainContainer::CountComponentControls() const
    {
    return 3; 
    }

// ---------------------------------------------------------
// CEasyBounceMainContainer::ComponentControl()
// 此函数被框架调用，返回指定的控件.
// ---------------------------------------------------------
//
CCoeControl* CEasyBounceMainContainer::ComponentControl(TInt aIndex) const
    {
    switch ( aIndex )
        {
        case 0:
        	return iLabel;
        	break;
        case 1:
        	return iStartButton;
        	break;
        case 2:
        	return iRankButton;
        	break;
        default:
            return NULL;
        }
    }


// ---------------------------------------------------------
// CEasyBounceMainContainer::OffeKeyEventL()
// 处理按键事件
// ---------------------------------------------------------
//
TKeyResponse CEasyBounceMainContainer::OfferKeyEventL(
    const TKeyEvent& /*aKeyEvent*/,TEventCode /*aType*/)
    {
    TKeyResponse ret;

    ret = EKeyWasNotConsumed;

    return ret;
    }

// ---------------------------------------------------------
// CEasyBounceMainContainer::Draw()
// 每次刷新屏幕的时候，重新绘制直线以及背景等
// ---------------------------------------------------------
//
void CEasyBounceMainContainer::Draw(const TRect& aRect) const
    {
    CWindowGc& gc = SystemGc();
    gc.SetPenStyle(CGraphicsContext::ENullPen);
    gc.SetBrushColor(KRgbWhite);
    gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    
    //使用当前皮肤来绘制背景
     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
     MAknsControlContext* cc = AknsDrawUtils::ControlContext( this );
     AknsDrawUtils::Background( skin, cc, this, gc, aRect );
    
    gc.DrawRect(aRect);
    }

// ---------------------------------------------------------
// CEasyBounceMainContainer::HandleControlEventL()
// 处理控件事件    
// ---------------------------------------------------------
//
void CEasyBounceMainContainer::HandleControlEventL(
    CCoeControl* aControl, TCoeEvent aEventType)
    {
    switch ( aEventType ) 
        { 
            case EEventStateChanged:
            {
				//获取mainview的指针
				CEasyBounceAppUi* appUi = (CEasyBounceAppUi*)iEikonEnv->AppUi();
				TUid mainViewId = TUid::Uid(EMainViewId); 
				CEasyBounceMainView* view = (CEasyBounceMainView*)appUi->View(mainViewId);
			
                if(aControl == iStartButton)
    			{
    			//进入游戏界面
    			view->CmdNewGameL();    			
    			}
    			else if(aControl == iRankButton)
    			{
                //进入排行榜界面
    			view->CmdShowRankL();
    			}
    		}
    		break; 
            default: 
                 break; 
    	}

    }

// ---------------------------------------------------------
// CEasyBounceMainContainer::MopSupplyObject()
// 此函数与其他函数一起用来使程序支持皮肤    
// ---------------------------------------------------------
//
TTypeUid::Ptr CEasyBounceMainContainer::MopSupplyObject(TTypeUid aId)
{
	 if (iBgContext )
	 {
	   return MAknsControlContext::SupplyMopObject( aId, iBgContext );
	 }
	 return CCoeControl::MopSupplyObject(aId);
}

// ---------------------------------------------------------
// CEasyBounceMainContainer::HandleResourceChange()
// 资源发生变化时被框架调用   
// ---------------------------------------------------------
//
void CEasyBounceMainContainer::HandleResourceChange(TInt aType)
	{
	//获取MainView的指针
	CEasyBounceAppUi* appUi = (CEasyBounceAppUi*)iEikonEnv->AppUi();
	TUid mainViewId = TUid::Uid(EMainViewId); 
	CEasyBounceMainView* mainView = (CEasyBounceMainView*)appUi->View(mainViewId);
	
	//获取view的区域大小，然后重设container的区域大小
	TRect mainViewRect = mainView->ClientRect();
	this->SetRect(mainViewRect);
	}

// ---------------------------------------------------------
// CEasyBounceMainContainer::CreateLableL()
// 测试函数
// ---------------------------------------------------------
//
void CEasyBounceMainContainer::CreateLableL()
	{
    iLabel= new (ELeave) CEikLabel;
    iLabel->SetContainerWindowL( *this );
    iLabel->SetTextL( _L("MainView") );
    iLabel->OverrideColorL( EColorLabelTextEmphasis, KRgbYellow );
    iLabel->SetEmphasis( CEikLabel::EPartialEmphasis ); 	
	}

// ---------------------------------------------------------
// CEasyBounceMainContainer::CreateButtonsL()
// 分别用两种方式创建了"start"和"rank"按钮
// ---------------------------------------------------------
//
void CEasyBounceMainContainer::CreateButtonsL()
	{
	//不使用资源文件，直接创建按钮
	//在按钮被创建的同时设置其显示区域
    _LIT(KStartButtonText,"Start");
    _LIT(KStartButtonHelpText,"");    
    iStartButton = CAknButton::NewL( NULL, NULL, NULL, NULL,
    		KStartButtonText, KStartButtonHelpText, 0, 0 );
    iStartButton->SetContainerWindowL(*this);
    iStartButton->SetRect(TRect ( TPoint(20,180), TSize(200,100) ) );
    iStartButton->SetObserver(this);
    iStartButton->MakeVisible(ETrue);
    iStartButton->ActivateL();

	//通过资源文件创建按钮
    //在SizeChanged()函数里对按钮的显示区域进行设置
    iRankButton = CAknButton::NewL();
	iRankButton->ConstructFromResourceL(R_RANK_BUTTON);
	iRankButton->SetContainerWindowL(*this);
	iRankButton->SetObserver(this);
	iRankButton->MakeVisible(ETrue);
	iRankButton->ActivateL();
	}

// 文件结束
